/*
 * @Description: 触发消息列表配置
 * @version: 电子名片2.0
 * @Author: 尹鑫
 * @Date: 2020-12-04 11:33:08
 * @LastEditors: 尹鑫
 * @LastEditTime: 2020-12-16 10:55:46
 */
function transformParams(editList, detail) {
  editList.forEach(item => {
    if (item.transform) {
      item.transform(detail);
    } else {
      item.value = detail[item.key]
    }
  })
}
export function configData() {
  const searchs = [
    { type: 'input', key: 'keyword', value: '', placeholder: '消息编号、消息标题' },
    {
      type: 'select', key: 'status', value: '', placeholder: '状态',
      selectList: [
        { value: '1', title: '已启用' },
        { value: '2', title: '已停用' }
      ]
    }
  ]
  const listHedar = [
    { prop: 'id', label: '定时任务编号' },
    { prop: 'jobName', label: '任务名称' },
    {
      prop: 'jobType', label: '任务类型',
      show: { '1': '自动标签生成', '2': '标签触发消息' }
    },
    { prop: 'triggerId', label: '任务模块', 
      show(data) {
        if (data.jobType == 1) {
          return data.tagName
        } else {
          return data.notifyTitle
        }
      }   
    },
    { prop: 'jobCron', label: '任务规则' },
    { prop: 'nextTime', label: '下次执行时间 ' },
    {
      prop: 'status', label: '状态',
      show: { '1': '已启用', '2': '已停用', default: '未启用' }
    },
    { prop: 'modifierName', label: '修改人' },
    { prop: 'gmtModified', label: '修改时间' },
    { prop: 'operate', label: '操作', isTemplate: true }
  ]
  return {
    searchs,
    listHedar
  }
}
export function createEditData(detail, templateList, getTagDownList) {
  const _selectItem = {};
  const editList = [
    { type: 'input', key: 'jobName', label: '任务名称：', value: '', have: true },
    {
      type: 'select', key: 'jobType', label: '任务类型：', value: '', have: true,
      options: [
        { value: 1, title: '自动标签计算' },
        { value: 2, title: '标签触发消息' }
      ],
      isLoad: false,
      change(val) {
        const _item = selectItem('triggerId');
        if (this.value == 1) {
          _item.options = getTagDownList;
        } else if (this.value == 2) {
          _item.options = templateList;
        }
        if (this.isLoad) _item.value = '';
        else this.isLoad = true;
      }
    },
    {
      type: 'select', key: 'triggerId', label: '任务模块：', value: '', have: true,
      options: [],
      setParams() {
        let _obj = {};
        const _type = editList.find(item => item.key === 'jobType').value;
        if (_type == 1) {
          _obj.tagId = this.value;
        } else {
          _obj.triggerId = this.value;
        }
        return _obj
      }
    },
    { 
      type: 'input', key: 'jobCron', label: '任务规则：', value: '', have: true, placeholder: '请输入Cron表达式',
      event: { fn: 'createRule', title: '生成表达式' }
    }
  ];
  if (detail) {
    transformParams(editList, detail);
  }
  editList.forEach(item => {
    if (item.change && !item.hideItem) item.change();
  })
  function selectItem(key) {
    if (key in _selectItem) {
      return _selectItem[key]
    } else {
      let _item = {};
      for (let i = 0; i < editList.length; i++) {
        const item = editList[i];
        if (item.key === key) {
          _item = item;
          break;
        }
      }
      _selectItem[key] = _item;
      return _selectItem[key];
    }
  }
  return editList;
}
